#pragma once
// #include <utility> // <--- 不再需要 swap

template <class T>
class mylist
{
public:
    class Node
    {
    public:
        Node* next;
        Node* prev;
        T data;

        Node() : next(nullptr), prev(nullptr) {}
        explicit Node(const T& val) : next(nullptr), prev(nullptr), data(val) {}
        Node(const Node& n) : next(nullptr), prev(nullptr), data(n.data) {}
        ~Node() = default;
    };

    class iterator
    {
    private:
        Node * iter;
        friend class mylist<T>;
    public:
        iterator() : iter(nullptr) {}
        explicit iterator(Node* p) : iter(p) {}
        ~iterator() = default;

        iterator& operator++ ()
        {
            if (iter) iter = iter->next;
            return *this;
        }

        iterator& operator--()
        {
            if (iter) iter = iter->prev;
            return *this;
        }

        iterator operator++(int)
        {
            iterator temp = *this;
            if (iter) iter = iter->next;
            return temp;
        }

        iterator operator--(int)
        {
            iterator temp = *this;
            if (iter) iter = iter->prev;
            return temp;
        }

        T& operator*()
        {
            return this->iter->data;
        }

        bool operator== (const iterator& x) const
        {
            return this->iter == x.iter;
        }

        bool operator!= (const iterator& x) const
        {
            return this->iter != x.iter;
        }
    };

    // 构造
    mylist() : head(nullptr), tail(nullptr), size(0) {}

    // 析构
    ~mylist()
    {
        while (this->head != nullptr)
        {
            Node* temp = this->head;
            this->head = this->head->next;
            delete temp;
        }
    }

    // [拷贝构造函数]
    // (这个实现是正确的，它通过 push_back 逐个复制元素)
    mylist (const mylist<T>& x) : head(nullptr), tail(nullptr), size(0)
    {
        Node* temp = x.head;
        while (temp != nullptr)
        {
            this->push_back(temp->data); // 重用 push_back 来构建新列表
            temp = temp->next;
        }
    }


    // *** [这就是你要求的实现] ***
    // [拷贝赋值运算符 - 不使用 swap]
    // 逻辑：1. 检查自赋值 2. 清空 this 3. 复制 x
    mylist<T>& operator=(const mylist<T>& x)
    {
        // 1. 自我赋值检查 (必须保留)
        if (this == &x)
        {
            return *this;
        }

        // 2. 清空 'this' 列表的旧内容
        // (重用 pop_front 来安全地 delete 每个节点)
        while (!this->empty())
        {
            this->pop_front();
        }
        // 此刻, 'this' 列表是空的 (head=nullptr, tail=nullptr, size=0)

        // 3. 从 'x' 复制新内容
        // (与拷贝构造函数完全相同的逻辑)
        Node* temp = x.head;
        while (temp != nullptr)
        {
            this->push_back(temp->data);
            temp = temp->next;
        }

        return *this;
    }

    void push_front (const T& x)
    {
        Node* temp = new Node(x);
        if (head == nullptr)
        {
            head = temp;
            tail = temp;
        }
        else
        {
            temp->next = head;
            head->prev = temp;
            head = temp;
        }
        ++size;
    }

    void push_back(const T& x)
    {
        Node* temp = new Node(x);
        if (head == nullptr)
        {
            head = temp;
            tail = temp;
        }
        else
        {
            tail->next = temp;
            temp->prev = tail;
            tail = temp;
        }
        ++size;
    }

    void pop_front()
    {
        if (head == nullptr) return;

        Node* temp = head;
        head = head->next;

        if (head == nullptr)
        {
            tail = nullptr;
        }
        else
        {
            head->prev = nullptr;
        }

        delete temp;
        --size;
    }

    void pop_back()
    {
        if (tail == nullptr) return;

        Node* temp = tail;
        tail = tail->prev;

        if (tail == nullptr)
        {
            head = nullptr;
        }
        else
        {
            tail->next = nullptr;
        }

        delete temp;
        --size;
    }

    iterator insert (iterator position, const T& x)
    {
        Node* posNode = position.iter;

        if (posNode == nullptr)
        {
            push_back(x);
            return iterator(tail);
        }

        if (posNode == head)
        {
            push_front(x);
            return iterator(head);
        }

        Node* temp = new Node(x);
        Node* prevNode = posNode->prev;

        temp->next = posNode;
        temp->prev = prevNode;

        prevNode->next = temp;
        posNode->prev = temp;

        ++size;
        return iterator(temp);
    }

    iterator erase(iterator position)
    {
        if (position.iter == nullptr) // 不能删除 end()
            return iterator(nullptr);

        // Case 1: 删除 head
        if (position.iter == head)
        {
            pop_front();
            // pop_front 之后，head 可能是 nullptr (如果列表变空了)
            // 或者 head 是新的头节点。无论哪种，iterator(head) 都是正确的。
            return iterator(head);
        }

        // Case 2: 删除 tail
        if (position.iter == tail)
        {
            pop_back();
            // pop_back 之后，我们总是返回 end() 迭代器
            return iterator(nullptr); // end()
        }

        // Case 3: 删除中间
        Node* temp = position.iter;
        Node* prevNode = temp->prev;
        Node* nextNode = temp->next;

        prevNode->next = nextNode;
        nextNode->prev = prevNode;

        delete temp;
        --size;
        return iterator(nextNode); // 返回下一个迭代器
    }

    iterator erase (iterator first, iterator last)
    {
        iterator it = first;
        while (it != last)
        {
            it = erase(it);
        }
        return last;
    }

    unsigned get_size() const
    {
        return this->size;
    }

    bool empty() const
    {
        return this->head == nullptr;
    }

    iterator begin()
    {
        return iterator(this->head);
    }

    iterator end()
    {
        return iterator(nullptr);
    }

    void splice (iterator position, mylist<T>& x)
    {
        if (x.empty()) return;

        Node* posNode = position.iter;

        if (this->empty())
        {
            // 如果 this 为空, 我们可以"窃取" x 的指针，这比 swap 更直接
            this->head = x.head;
            this->tail = x.tail;
            this->size = x.size;

            x.head = nullptr;
            x.tail = nullptr;
            x.size = 0;
            return;
        }

        if (posNode == nullptr)
        {
            this->tail->next = x.head;
            x.head->prev = this->tail;
            this->tail = x.tail;
        }
        else if (posNode == head)
        {
            this->head->prev = x.tail;
            x.tail->next = this->head;
            this->head = x.head;
        }
        else
        {
            Node* prevNode = posNode->prev;
            prevNode->next = x.head;
            x.head->prev = prevNode;
            x.tail->next = posNode;
            posNode->prev = x.tail;
        }

        this->size += x.size;

        x.head = nullptr;
        x.tail = nullptr;
        x.size = 0;
    }


private:
    Node * head;
    Node * tail;
    unsigned size;
};